<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>工时计算器</title>
    <!-- 引入 Layui 的 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/layui/2.8.11/css/layui.min.css">
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
            max-width: 100%;
            margin: 0;
            padding: 15px;
            box-sizing: border-box;
            background-color: #f0f2f5;
        }

        .layui-container {
            background-color: white;
            padding: 15px;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
        }

        .layui-elem-quote {
            margin: 0 0 15px;
            padding: 10px;
            font-size: 18px;
            border-left: 3px solid #1890ff;
            background-color: #fafafa;
        }

        .layui-form-item {
            margin-bottom: 15px;
        }

        .layui-form-label {
            width: 80px;
            padding: 9px 0;
            font-size: 14px;
        }

        .layui-input-block {
            margin-left: 95px;
        }

        .layui-input {
            height: 40px;
            border-radius: 4px;
            font-size: 14px;
        }

        .layui-btn {
            width: 100%;
            /* 按钮宽度占满父容器 */
            height: 40px;
            font-size: 16px;
            margin-bottom: 10px;
            /* 按钮之间添加间距 */
        }

        /* 移除按钮之间的额外左边距 */
        .layui-btn+.layui-btn {
            margin-left: 0;
        }

        #result {
            margin-top: 15px;
            padding: 10px;
            background-color: #f6ffed;
            border: 1px solid #b7eb8f;
            border-radius: 4px;
            font-size: 14px;
            color: #52c41a;
            display: none;
            /* 默认隐藏结果框 */
        }

        @media (max-width: 480px) {
            .layui-form-label {
                width: 100%;
                display: block;
                text-align: left;
                padding: 0 0 5px;
            }

            .layui-input-block {
                margin-left: 0;
            }
        }
    </style>
</head>

<body>
    <div class="layui-container">
        <h1 class="layui-elem-quote">每日工时计算</h1>
        <form class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">上班时间</label>
                <div class="layui-input-block">
                    <input type="text" id="startTime" class="layui-input" placeholder="选择日期时间">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">下班时间</label>
                <div class="layui-input-block">
                    <input type="text" id="endTime" class="layui-input" placeholder="选择日期时间">
                </div>
            </div>
            <div class="layui-form-item" style="margin-bottom: 0px;">
                <label class="layui-form-label">中午休息时长</label>
                <div class="layui-input-block">
                    <input type="number" id="lunchBreak" class="layui-input" value="1" step="0.1" min="0">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <div id="result" style="margin-bottom: 9px; margin-top: 0;"></div>
                    <button class="layui-btn" lay-submit lay-filter="calculate">计算</button>
                    <button class="layui-btn layui-btn-primary" type="button" onclick="clearForm()">清空</button>
                </div>
            </div>
        </form>

    </div>

    <!-- 引入 Layui 的 JS 文件 -->
    <script src="https://cdn.staticfile.org/layui/2.8.11/layui.min.js"></script>
    <script>
        layui.use(['form', 'laydate'], function () {
            const form = layui.form;
            const laydate = layui.laydate;

            // 初始化日期时间选择器，修改为只选择时间
            laydate.render({
                elem: '#startTime',
                type: 'time',
                format: 'HH:mm:ss'
            });

            // 获取当前时间的时分秒
            const currentDate = new Date();
            const hour = String(currentDate.getHours()).padStart(2, '0');
            const minute = String(currentDate.getMinutes()).padStart(2, '0');
            const second = String(currentDate.getSeconds()).padStart(2, '0');
            const currentTime = `${hour}:${minute}:${second}`;

            laydate.render({
                elem: '#endTime',
                type: 'time',
                format: 'HH:mm:ss',
                value: currentTime // 设置默认值为当前时间
            });

            // 监听表单提交事件
            form.on('submit(calculate)', function (data) {
                const startTime = document.getElementById('startTime').value;
                const endTime = document.getElementById('endTime').value;
                const lunchBreakInput = document.getElementById('lunchBreak').value;

                const resultDiv = document.getElementById('result');

                // 检查上班时间和下班时间是否输入
                if (!startTime || !endTime) {
                    resultDiv.innerHTML = '请输入上班时间与下班时间。';
                    // 设置错误提示样式
                    resultDiv.style.backgroundColor = '#fff1f0';
                    resultDiv.style.borderColor = '#ffccc7';
                    resultDiv.style.color = '#ff4d4f';
                    resultDiv.style.display = 'block';
                    return false;
                }

                const currentDate = new Date();
                const year = currentDate.getFullYear();
                const month = currentDate.getMonth();
                const day = currentDate.getDate();

                const start = new Date(year, month, day, ...startTime.split(':').map(Number));
                const end = new Date(year, month, day, ...endTime.split(':').map(Number));
                const lunchBreak = lunchBreakInput ? parseFloat(lunchBreakInput) : 0;

                // 检查日期是否有效
                if (isNaN(start.getTime()) || isNaN(end.getTime())) {
                    resultDiv.innerHTML = '请输入有效的时间。';
                    // 设置错误提示样式
                    resultDiv.style.backgroundColor = '#fff1f0';
                    resultDiv.style.borderColor = '#ffccc7';
                    resultDiv.style.color = '#ff4d4f';
                } else {
                    // 恢复正常提示样式
                    resultDiv.style.backgroundColor = '#f6ffed';
                    resultDiv.style.borderColor = '#b7eb8f';
                    resultDiv.style.color = '#52c41a';

                    // 计算总的时间差（毫秒）
                    const totalMilliseconds = end - start;

                    // 计算中午休息时间（毫秒）
                    const lunchBreakMilliseconds = lunchBreak * 3600 * 1000;

                    // 计算实际工作时间（毫秒）
                    const workMilliseconds = totalMilliseconds - lunchBreakMilliseconds;

                    // 将实际工作时间转换为小时
                    const workHours = workMilliseconds / (1000 * 60 * 60);

                    // 保留一位小数并四舍五入
                    const roundedWorkHours = workHours.toFixed(1);
                    const resultText = `当天实际工作时间为: ${roundedWorkHours} 小时`;
                    resultDiv.innerHTML = resultText;

                    // 仅复制时间数值到剪贴板
                    if (navigator.clipboard) {
                        navigator.clipboard.writeText(roundedWorkHours).then(() => {
                            console.log('时间数值已复制到剪贴板');
                        }).catch((err) => {
                            console.error('复制失败: ', err);
                        });
                    } else {
                        console.error('当前浏览器不支持剪贴板 API');
                    }
                }
                resultDiv.style.display = 'block'; // 显示结果框
                return false;
            });
        });

        // 清空表单和结果
        function clearForm() {
            document.getElementById('startTime').value = '';
            const currentDate = new Date();
            const hour = String(currentDate.getHours()).padStart(2, '0');
            const minute = String(currentDate.getMinutes()).padStart(2, '0');
            const second = String(currentDate.getSeconds()).padStart(2, '0');
            const currentTime = `${hour}:${minute}:${second}`;
            document.getElementById('endTime').value = currentTime;
            document.getElementById('lunchBreak').value = 1;
            document.getElementById('result').style.display = 'none';
            document.getElementById('result').innerHTML = '';
            // 恢复默认样式
            document.getElementById('result').style.backgroundColor = '#f6ffed';
            document.getElementById('result').style.borderColor = '#b7eb8f';
            document.getElementById('result').style.color = '#52c41a';
        }
    </script>
</body>

</html>